package com.zff.rental.service.apartment;


import com.zff.rental.common.ResultCode;
import com.zff.rental.entity.apartment.DeviceDictEntity;
import com.zff.rental.mapstruct.apartment.DemoMapstruct;
import com.zff.rental.mapstruct.apartment.DeviceMapstruct;
import com.zff.rental.service.BaseService;
import com.zff.rental.vo.apartment.deviceDict.AddDeviceDictParams;
import com.zff.rental.vo.apartment.deviceDict.DeviceDictVo;
import com.zff.rental.vo.apartment.deviceDict.ModifyDeviceDictParams;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author HCY
 */
@Service
public class DeviceDictServer extends BaseService {

    @Transactional
    public DeviceDictVo add(AddDeviceDictParams params) {
        DeviceDictEntity map = DemoMapstruct.INSTANCE.map(params);
        DeviceDictEntity saveResult = deviceDictRepository.save(map);
        return DeviceMapstruct.INSTANCE.map(saveResult);
    }

    public DeviceDictVo modify(ModifyDeviceDictParams params) {
        Long id = params.getId();
        DeviceDictEntity findResult = deviceDictRepository.findById(id).orElseThrow(ResultCode.RECORD_NOT_EXISTS::toException);
        if (findResult == null) {
            throw ResultCode.RECORD_NOT_EXISTS.toException();
        }
        DemoMapstruct.INSTANCE.merge(findResult, params);
        DeviceDictEntity saveResult = deviceDictRepository.save(findResult);
        return DeviceMapstruct.INSTANCE.map(saveResult);
    }

    public List<DeviceDictVo> batchModify(List<ModifyDeviceDictParams> params) {
        List<Long> idList = params.stream().map(ModifyDeviceDictParams::getId).collect(Collectors.toList());

        List<DeviceDictEntity> findResult = deviceDictRepository.findAllById(idList);
        Map<Long, DeviceDictEntity> findResultMap = findResult.stream()
                .collect(Collectors.toMap(DeviceDictEntity::getId, Function.identity(), (key1, key2) -> key1));
        Map<Long, ModifyDeviceDictParams> paramsMap = params.stream()
                .collect(Collectors.toMap(ModifyDeviceDictParams::getId, Function.identity(), (key1, key2) -> key1));

        for (DeviceDictEntity tem : findResult) {
            DemoMapstruct.INSTANCE.merge(findResultMap.get(tem.getId()), paramsMap.get(tem.getId()));
        }

        List<DeviceDictEntity> saveResult = deviceDictRepository.saveAll(findResult);
        return DeviceMapstruct.INSTANCE.map(saveResult);
    }

    public Boolean remove(Long id) {
        deviceDictRepository.deleteById(id);
        return true;
    }

    public DeviceDictVo details(Long id) {
        DeviceDictEntity findResult = deviceDictRepository.findById(id).orElseThrow(ResultCode.RECORD_NOT_EXISTS::toException);
        if (findResult == null) {
            throw ResultCode.RECORD_NOT_EXISTS.toException();
        }

        return DemoMapstruct.INSTANCE.map(findResult);
    }

    public List<DeviceDictVo> all() {
        List<DeviceDictEntity> records = deviceDictRepository.findAll();
        return DemoMapstruct.INSTANCE.mapAsList(records);
    }
}
